﻿using System;
using System.Net;
using System.Net.Sockets;

namespace AutojsController.Helpers
{
    public static class Checks
    {
        /// <summary>
        /// 检查是否为正常的IP地址（非回环/掩码/广播地址）
        /// 暂时只考虑IPV4
        /// </summary>
        public static bool IsNormalIPAddress(IPAddress ip)
        {
            if (ip == null) return false;
            if (IPAddress.IsLoopback(ip)) return false; //回环地址
            if (ip.AddressFamily != AddressFamily.InterNetwork) return false; //非IPV4
            var bytes = ip.GetAddressBytes();
            if (bytes[0] == 127) return false;   //回环地址
            if (bytes[0] == 0) return false;     //0开头的地址
            if (bytes[0] == 169 && bytes[1] == 254) return false; //169.254.x.x 地址
            if (bytes[0] >= 224 && bytes[0] <= 239) return false;   //多播地址和广播地址
            if (bytes[0] >= 240) return false; //保留地址
            return true;
        }
    }
}
